package com.agile.familytree.business;

import java.io.BufferedReader;
import java.util.HashMap;
import java.util.List;

import com.agile.familytree.datastructure.Family;
import com.agile.familytree.datastructure.Person;
import com.agile.familytree.util.FamilyUtil;

public class BeanProducer{
		
	public void processData(BufferedReader reader, List<Person> lPerson, List<Family> lFamily) throws Exception{
		
		Person person = new Person();
		Family family = new Family();
		
		HashMap<String, String> hValidTags = new HashMap<String, String>();
		
		String line = null;
		String[] parts ;
		String tagName="";
		String tagValue = "";
		
		int currentNum = 0;
		
		boolean isDOB = false;
		boolean isDOD = false;
		boolean isMarrDate = false;
		boolean isDivorceDate = false;
		
		populateValidTags(hValidTags);

		while ((line = reader.readLine()) != null) {
		    
			//System.out.println("-----------------------------");
			//System.out.println(line);
			parts = line.split(" ");
			
			//check for previous tag name
			if(tagName!= null && tagName.equals("BIRT")){
				isDOB = true;
			}			
			if(tagName!= null && tagName.equals("DEAT")){
				isDOD = true;
			}
			if(tagName!= null && tagName.equals("MARR")){
				isMarrDate = true;
			}
			if(tagName!= null && tagName.equals("DIV")){
				isDivorceDate = true;
			}

			final String sInvalidTag = "Invalid tag";
			boolean isNewperson = false;
			
			for ( int i = 0 ; i < parts.length ; i++){
				
				//first column - number
				if(i==0){

					//check for new person
					if(Integer.parseInt(parts[i])==0){
						isNewperson = true;
					}
					else
					{
						isNewperson = false;
					}
					
					
					///////////////////////////////////////////////
					//Initialize for new person
					///////////////////////////////////////////////
					if(isNewperson){

						//System.out.println("##############################################");
						
						if(person.getIdentifier() != null && person.getIdentifier().length()>0)	{
							
							if(person.getIdType().trim().equals("INDI")) 
								lPerson.add(person);
							
							 FamilyUtil.getInstance().populateIdName(person.getIdentifier().trim(), person.getName().trim());
							 FamilyUtil.getInstance().populateIdAlive(person.getIdentifier().trim(), person.getDeat().trim());

						}
						
						if(family.getIdentifier() != null && family.getIdentifier().length()>0)	{
							
							if(family.getIdType().trim().equals("FAM")) 
								lFamily.add(family);
							
						}
						
						person = new Person();
						family = new Family();
						
						//initialize for new person
						isNewperson = false;
						isDOB = false;
						isDOD = false;
						isMarrDate = false;
						isDivorceDate = false;

					}
					
					currentNum = Integer.parseInt(parts[i]);
					//System.out.println(currentNum);
					
				//second column - Tag name	
				}else if(i==1 ){

					try{
						
						//validate the tag
						tagName = hValidTags.get(parts[i]);

						//invalid tag
						if(tagName == null){
							//System.out.println(sInvalidTag);
						}
						///////////////////////////////////////////////
						//print out the tag name
						///////////////////////////////////////////////
						else{
							//System.out.println("tagname:"+tagName);
						}
						
						if(currentNum == 0 ){
							
							person.setIdentifier(parts[i]);
							family.setIdentifier(parts[i]);
						}
					}
					catch(Exception e){
						System.out.println("Exception thrown while working on Tag, please investigate..");
						e.printStackTrace();
						continue;
					}
					
				}
				else{
					tagValue = tagValue+parts[i]+" ";

				}
			}//end of for loop for each column in the same line
			
			//System.out.println("tag value"+tagValue);
			//System.out.println("tag name:"+tagName);
			
			populateBean(person, family, tagName, tagValue, isDOB, isDOD, isMarrDate, isDivorceDate, currentNum);
			
			if(isDOB = true ) isDOB = false;
			if(isDOD = true) isDOD = false;
			if(isMarrDate = true ) isMarrDate = false;
			if(isDivorceDate = true) isDivorceDate = false;

			tagValue = "";
		}
		
	}
	
	private void populateBean(Person person, Family family, String tagName, String tagValue, boolean isDOB, boolean isDOD, boolean isDOM,boolean isDODiv, int currentNum){

		//populate person bean
		if(tagName != null && tagName.equals("INDI")){
			person.setIndi(tagValue);
		}
		else if(tagName != null && tagName.equals("NAME")){
			person.setName(tagValue);
		}
		else if(tagName != null && tagName.equals("SEX")){
			person.setSex(tagValue);
		}
		else if(tagName != null &&  tagName.equals("FAMC")){
			person.setFamc(tagValue);
		}
		else if(tagName != null && tagName.equals("FAMS")){
			person.setFams(tagValue);
		}
		else if(tagName != null && tagName.equals("TRLR")){
			person.setTrlr(tagValue);
		}
		else if(tagName != null && tagName.equals("NOTE")){
			person.setNote(tagValue);
		}
		else if(tagName != null && tagName.trim().equals("_CURRENT")){
			person.setDiv(tagValue);
		}
		
		//populate family bean
		else if(tagName != null && tagName.trim().equals("FAM")){
			family.setFam(tagValue);
		}
		else if(tagName != null && tagName.equals("HUSB")){
			family.setHusb(tagValue);
		}
		else if(tagName != null && tagName.equals("WIFE")){
			family.setWife(tagValue);
		}
		else if(tagName != null && tagName.equals("CHIL")){
			family.setChil(tagValue);
		}

		//set Id Type if it's 'INDI' or 'FAM'
		if(currentNum == 0 ){
			
			if(tagValue.trim().equals("FAM")){
				family.setIdType(tagValue);
			}
			else if(tagValue.trim().equals("INDI")){
				person.setIdType(tagValue);
			}
		}

		if(isDOB) person.setBirt(tagValue);
		if(isDOD) person.setDeat(tagValue);
		if(isDOM) family.setMarr(tagValue);
		if(isDODiv) family.setDiv(tagValue);
		

	}
	
	static final void populateValidTags(HashMap<String, String> hm){		
		
		hm.put("INDI", "INDI");
		hm.put("NAME", "NAME");
		hm.put("SEX", "SEX");
		hm.put("BIRT", "BIRT");
		hm.put("DEAT", "DEAT");
		hm.put("FAMC", "FAMC");
		hm.put("FAMS", "FAMS");
		hm.put("FAM", "FAM");
		hm.put("MARR", "MARR");
		hm.put("HUSB", "HUSB");
		hm.put("WIFE", "WIFE");
		hm.put("CHIL", "CHIL");
		hm.put("DIV", "DIV");
		hm.put("_CURRENT", "_CURRENT");
		hm.put("DATE", "DATE");
		hm.put("TRLR", "TRLR");
		hm.put("NOTE", "NOTE");
	}

	

}
